package cn.edu.cqupt.cxcy.action;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.edu.cqupt.cxcy.dto.IndexImpl;
import cn.edu.cqupt.cxcy.model.Team;
import cn.edu.cqupt.cxcy.model.Tutor;
import cn.edu.cqupt.cxcy.util.Tools;
import cn.edu.cqupt.cxcy.util.Validate;

/**
 * 主页-【创业辅导】
 * @author Dancer-mno
 * @date 2015年10月17日
 */
@SuppressWarnings("serial")
public class ZhuceInfo extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		this.doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse resp) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		resp.setContentType("text/html;charset=utf-8");
		String type = request.getParameter("type");//1：导师；2：团队
		String name = request.getParameter("name");
		String loname = request.getParameter("loname");
		String password = Tools.getMD5(Tools.getMD5(request.getParameter("password")));
		String code = request.getParameter("code");
		System.out.println(type + "," + name + "," + loname + "," + password + "," + code + ",");
		if(name == null || "".equals(name) || !Validate.checkEmail(loname) || !new IndexImpl().usernameIsUnique(loname) || !Validate.stringIsNO(type) || password.equals(Tools.getMD5(Tools.getMD5("")))) {
			resp.sendRedirect(request.getHeader("referer"));
			return;
		}
		if (!((String)request.getSession().getAttribute("safecode")).equalsIgnoreCase(code)) { //验证验证码
			resp.sendRedirect(request.getHeader("referer"));
			return;
		}
		int flag = Integer.valueOf(type);
		if(flag == 1) {
			Tutor teac = new Tutor();
			teac.setTu_name(name);
			teac.setTu_loname(loname);
			teac.setTu_passw(password);
			new IndexImpl().addTeac(teac);
		} else if (flag == 2) {
			Team team = new Team();
			team.setTe_name(name);
			team.setTe_loname(loname);
			team.setTe_passw(password);
			new IndexImpl().addTeam(team);
		} 
		resp.sendRedirect("index");
	}

}
